import numpy as np

###1 操作数组的便捷性
price = np.array([3, 4, 5, 9])
price = price + 1
print(price)

fruitPrice = np.array([5, 4, 6, 2])
fruitPrice = fruitPrice + 1
print(fruitPrice)

###2 性能上的体现

import numpy as np
import time

arr = np.random.rand(100000)
# 使用Numpy进行平方运算
start_time = time.time()
arr_squared = arr ** 2
print("Numpy平方运算时间：", time.time() - start_time)
# 使用循环进行平方运算
start_time = time.time()
arr_squared = []
for i in range(len(arr)):
    arr_squared.append(arr[i] ** 2)
print("循环平方运算时间：", time.time() - start_time)

### 使用pandas 和 matplotlib 画图

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# 设置中文字体，防止matplotlib中文乱码
plt.rcParams['font.family'] = 'Microsoft YaHei'
plt.rcParams['axes.unicode_minus'] = False

# 读取excel中的数据
df = pd.read_excel(r'.\data\test.xlsx', names=['time', 'SSTA'])
# print(df)
X = df['time']
Y = df['SSTA']
c = 0.5
print(f"Y.shape[0]: {Y.shape[0]}")
y_above = np.zeros(Y.shape[0])
y_below = np.zeros(Y.shape[0])
for i in range(Y.shape[0]):
    if abs(Y[i]) >= c:
        y_above[i] = Y[i]
    else:
        y_below[i] = Y[i]

# 设置色带，达到根据数据变色
# map_vir = plt.get_cmap(name='Paired')
# colors = map_vir(Y)
# 设置标题，格式
plt.tight_layout()
plt.title("气温数据图")
plt.xlabel("Time")
plt.xticks(rotation=100, fontsize=6)
plt.ylabel("SSTA")
## plt.bar(x, height, width=0.8, bottom=None, align='center', **kwargs)
## x表示 x轴的值，height 高度，wodth 宽度，color 颜色，
plt.bar(X, y_above, width=5.0, color='red', label="Above average")
plt.bar(X, y_below, width=5.0, color='grey', label="below average")
# 其他线应该在画柱状图之后再画
# linestyle ：'-'代表实线，'--'代表虚线，'-.'代表点划线，':'代表点划线
plt.axhline(y=c, color='black', linestyle=':')
plt.savefig(r"./data/shares_bar.png")
print("柱状图生成成功！请在data目录下查看")
